/**
 * 
 */
package de.cbf.cam.dsa.tavern.carte.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.IEditableEntity;
import de.cbf.cam.dsa.tavern.carte.ui.interfaces.ICompositeEditEntity;

/**
 * @author Stefan Alter
 *
 */
public abstract class AbstractCompositeEditEntity extends Composite implements
		ICompositeEditEntity {
	private Composite	compositeEditEntity	= null;	
	private Composite	compositeButtons	= null;
	private Button		buttonSave			= null;
	private Button		buttonCancel		= null;
	/**
	 * @param parent
	 * @param style
	 */
	public AbstractCompositeEditEntity(Composite parent, int style) {
		super(parent, style);
		initialize();		
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        GridLayout layout = new GridLayout();
        	layout.numColumns		= 1;
        	layout.marginHeight		= 0;
        	layout.marginWidth		= 0;
        	layout.verticalSpacing	= 1; 	
        //	
        this.setLayout(layout);
        //
        createCompositeEditEntity();       
        createCompositeButtons();
        //
		registerListeners();
		//
		init();
	}
	/**
	 * This method initializes compositeEditEntity	
	 *
	 */
	private void createCompositeEditEntity() {
		GridLayout layoutCompositeEditEntity = new GridLayout();
			layoutCompositeEditEntity.numColumns 		= 1;
			layoutCompositeEditEntity.horizontalSpacing = 0;
			layoutCompositeEditEntity.verticalSpacing	= 0;
			layoutCompositeEditEntity.marginWidth		= 0;
			layoutCompositeEditEntity.marginHeight		= 0;		
		GridData gDCompositeEditEntity = new GridData();			
			gDCompositeEditEntity.horizontalAlignment		= GridData.FILL;
			gDCompositeEditEntity.verticalAlignment			= GridData.FILL;
			gDCompositeEditEntity.grabExcessHorizontalSpace = true;
			gDCompositeEditEntity.grabExcessVerticalSpace	= true;
		//
		compositeEditEntity = new Composite(this, SWT.NONE);
		compositeEditEntity.setLayout(layoutCompositeEditEntity);
		compositeEditEntity.setLayoutData(gDCompositeEditEntity);
		//
		Composite composite = createCompositeEntity(compositeEditEntity);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
	}

	/**
	 * This method initializes compositeButtons
	 */
	private void createCompositeButtons() {
		GridLayout layoutCompositeButtons = new GridLayout();
			layoutCompositeButtons.numColumns				= 2;
			layoutCompositeButtons.marginHeight				= 0;
			layoutCompositeButtons.marginWidth				= 0;
			layoutCompositeButtons.verticalSpacing			= 0;
			layoutCompositeButtons.horizontalSpacing		= 2;			
		GridData gDCompositeButtons = new GridData();
			gDCompositeButtons.horizontalAlignment			= GridData.FILL;
			gDCompositeButtons.verticalAlignment			= GridData.CENTER;
			gDCompositeButtons.grabExcessHorizontalSpace	= true;
			gDCompositeButtons.grabExcessVerticalSpace		= false;		
		GridData gDBtnSave = new GridData();
			gDBtnSave.horizontalAlignment					= GridData.END;
			gDBtnSave.verticalAlignment						= GridData.CENTER;
			gDBtnSave.grabExcessHorizontalSpace				= true;			
		GridData gDBtnCancel = new GridData();
			gDBtnCancel.horizontalAlignment					= GridData.END;
			gDBtnCancel.verticalAlignment					= GridData.CENTER;
		//	
		compositeButtons = new Composite(this, SWT.NONE);	
		compositeButtons.setLayout(layoutCompositeButtons);		
		compositeButtons.setLayoutData(gDCompositeButtons);		
		//
		buttonSave = new Button(compositeButtons, SWT.NONE);
		buttonSave.setLayoutData(gDBtnSave);
		buttonSave.setText("Speichern");	
		//
		buttonCancel = new Button(compositeButtons, SWT.NONE);
		buttonCancel.setLayoutData(gDBtnCancel);
		buttonCancel.setText("Abbrechen");		
	}

	private void registerListeners() {
		buttonCancel.addSelectionListener(cancelEdit);
		buttonSave.addSelectionListener(saveEdit);		
	}

	private void init() {
		buttonSave.setEnabled(false);
		buttonCancel.setEnabled(false);
	}

	protected abstract boolean		checkEntity(IEditableEntity value);
	protected abstract void			setEntityData(IEditableEntity value);
	protected abstract Composite	createCompositeEntity(Composite parent);
	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.tavern.carte.ui.interfaces.ICompositeEditEntity#setEntity(de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.INamedEntity)
	 */
	@Override
	public final void setEntity(IEditableEntity value) throws IllegalArgumentException {
		if(checkEntity(value)){
			setEntityData(value);
		}		
	}
	
	public void setButtonSaveEnabled(boolean value){
		buttonSave.setEnabled(value);
	}	
	public void setButtonCancelEnabled(boolean value){
		buttonCancel.setEnabled(value);
	}	
	@Override
	public void setEnabled(boolean enabled) {
		if( ! enabled){			//buttons are only switched off
			buttonSave.setEnabled(enabled);
			buttonCancel.setEnabled(enabled);			
		}	
		super.setEnabled(enabled);
	}
	
	private Collection<ActionListener> saveEditListeners = new ArrayList<ActionListener>();  //  @jve:decl-index=0:
	public boolean registerSaveEditListener(ActionListener listener){
		return saveEditListeners.add(listener);
	}
	public boolean deRegisterSaveEditListener(ActionListener listener){
		return saveEditListeners.remove(listener);
	}
	protected SaveEditAdvancer saveEdit = new SaveEditAdvancer();
	private class SaveEditAdvancer extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			ActionEvent event = new ActionEvent(AbstractCompositeEditEntity.this, ActionEvent.ACTION_PERFORMED, "SaveEdit");
			for (ActionListener listener : saveEditListeners) {
				listener.actionPerformed(event);
			}
		}		
	}
	private Collection<ActionListener> cancelEditListeners = new ArrayList<ActionListener>();
	public boolean registerCancelEditListener(ActionListener listener){
		return cancelEditListeners.add(listener);
	}
	public boolean deRegisterCancelEditListener(ActionListener listener){
		return cancelEditListeners.remove(listener);
	}
	protected CancelEditAdvancer cancelEdit = new CancelEditAdvancer();
	private class CancelEditAdvancer extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			ActionEvent event = new ActionEvent(AbstractCompositeEditEntity.this, ActionEvent.ACTION_PERFORMED, "CancelEdit");
			for (ActionListener listener : cancelEditListeners) {
				listener.actionPerformed(event);
			}
		}		
	}	
}  //  @jve:decl-index=0:visual-constraint="10,10"
